package top.codedance.iotp.server.net.handle.util;

import top.codedance.iotp.common.entity.DeviceEntity;
import top.codedance.iotp.server.ServerApplication;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class DeviceService {
    private static Logger logger = ServerApplication.getLogger(DeviceService.class);

    /**
     * 检测提供的设备ID是否是合法设备
     * @param deviceEntity
     * @return
     */
    public static boolean isExits(DeviceEntity deviceEntity){
        try {
            return ServerApplication.deviceDaoUtil.has(deviceEntity);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 检测提供的设备ID是否是合法设备
     * @param deviceEntity
     * @return
     */
    public static boolean isExits(DeviceEntity deviceEntity, String privateKey){
        try {
            return ServerApplication.deviceDaoUtil.has(deviceEntity, privateKey);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 添加合法设备信息
     * @param deviceEntity
     */
    public static void add(DeviceEntity deviceEntity){
        try {
            ServerApplication.deviceDaoUtil.add(deviceEntity);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

    public static DeviceEntity getDevice(String deviceId){
        DeviceEntity paramEntity = new DeviceEntity();
        paramEntity.setDeviceId(deviceId);
        try {
            return (DeviceEntity) ServerApplication.deviceDaoUtil.getEntity(paramEntity);
        }catch (Exception e){
            logger.debug(e.getMessage());
            return null;
        }
    }

    public static DeviceEntity getDevice(String deviceId, String privateKey){
        DeviceEntity paramEntity = new DeviceEntity();
        paramEntity.setDeviceId(deviceId);
        try {
            return (DeviceEntity) ServerApplication.deviceDaoUtil.getEntity(paramEntity, privateKey);
        }catch (Exception e){
            logger.debug(e.getMessage());
            return null;
        }
    }

    /**
     * 查询所有合法设备
     * @return
     */
    public static List<DeviceEntity> list(){
        try {
            return ServerApplication.deviceDaoUtil.selectAll();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return new ArrayList<DeviceEntity>();
        }
    }

    /**
     * 查询所有合法设备
     * @return
     */
    public static List<DeviceEntity> list(String privateKey){
        try {
            return ServerApplication.deviceDaoUtil.selectAll(privateKey);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return new ArrayList<DeviceEntity>();
        }
    }

    /**
     * 删除设备
     * @param id
     */
    public static void delete(int id){
        try {
            ServerApplication.deviceDaoUtil.delete(id);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
}
